package cn.sxt.clinic.controller;


import cn.sxt.clinic.pojo.*;
import cn.sxt.clinic.service.IDeptService;
import cn.sxt.clinic.service.IDoctorService;
import cn.sxt.clinic.service.IRegisteredService;
import cn.sxt.clinic.service.IRegistrationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/registeredList")
public class RegisteredListController {

    @Autowired
    private IRegisteredService registeredService;

    @Autowired
    private IDoctorService doctorService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IRegistrationService registrationService;

    @RequestMapping("/registeredListManager")
    public String registeredListManager(){
        return "registeredListManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public ResultObject list(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize){

        //分页对象
        Page<Registered> page = new Page<>(pageNum, pageSize);
        //条件对象
        QueryWrapper<Registered> queryWrapper = new QueryWrapper<>();

        //查询结果
        Page<Registered> result = registeredService.page(page,queryWrapper);
        return new ResultObject(result);
    }


    @RequestMapping("/delete")
    @ResponseBody
    public MessageObject delete(long registeredId){

        boolean flag = registeredService.removeById(registeredId);

        MessageObject mo = new MessageObject(1, "删除数据成功");
        if (!flag){
            mo = new MessageObject(0, "删除数据失败,请联系管理员");
        }
        return mo;
    }

    @RequestMapping("/registeredListEdit")
    public String registeredListEdit(Model m, Long registeredId){
        //如果id为空，添加，为空，修改
        if(registeredId !=null){
            Registered registered = registeredService.getById(registeredId);
            m.addAttribute("registered",registered);


            QueryWrapper<Dept> wrapper1 = new QueryWrapper<>();
            List<Dept> deptName = deptService.list(wrapper1);

            m.addAttribute("deptName",deptName);


            QueryWrapper<Doctor> wrapper2 = new QueryWrapper<>();

            List<Doctor> doctors = doctorService.list(wrapper2);

            m.addAttribute("doctors",doctors);


            QueryWrapper<Registration> wrapper3 = new QueryWrapper<>();

            List<Registration> registrationType = registrationService.list(wrapper3);

            m.addAttribute("registrationType",registrationType);


            QueryWrapper<Registration> wrapper4 = new QueryWrapper<>();

            List<Registration> registrationMoneys = registrationService.list(wrapper4);

            m.addAttribute("registrationMoneys",registrationMoneys);
        }

        return "registeredListEdit";
    }

    @RequestMapping("/update")
    @ResponseBody
    public MessageObject update(Registered registered){

        //修改挂号列表
        boolean flag = registeredService.saveOrUpdate(registered);


        MessageObject mo = new MessageObject(1, "修改数据成功");
        if(!flag){
            mo = new MessageObject(0,"修改数据失败，请联系管理员");
        }
        return mo;
    }

}
